package com.qianchang.microfinance.view.mine;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.OverListBean;
import com.qianchang.microfinance.bean.RechargeListItemBean;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RechargeFixRvAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private LayoutInflater inflater;
    private SettleButtonInterface settleButtonInterface;
    private RepayMentButtonInterface repayMentButtonInterface;
    private RechageButtonInterface rechageButtonInterface;
    private List<RechargeListItemBean> listRechargeItemBean;



    public RechargeFixRvAdapter(Context mContext){
        this.context = mContext;
        inflater = LayoutInflater.from(context);
        listRechargeItemBean = new ArrayList<RechargeListItemBean>();
    }

    public void setData(List<RechargeListItemBean> list){
        if(list != null){
            listRechargeItemBean = list;
        }
        notifyDataSetChanged();
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_rechargefix_layout,parent,false);
        RechaFixHolder rechaFixHolder = new RechaFixHolder(view);
        return rechaFixHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        RechargeListItemBean rechargeListItemBean = listRechargeItemBean.get(position);
        List<OverListBean>  overListBeans = rechargeListItemBean.getOverlist();
        RechaFixHolder rechaFixHolder = (RechaFixHolder)holder;

        rechaFixHolder.tvContent.setText("是否提前结清，结清金额 " + rechargeListItemBean.getAll()  + " 元");
        rechaFixHolder.tvOdTitle.setText("您目前有" + overListBeans.size() + "笔逾期");
        setDataToText(overListBeans,rechaFixHolder.tvOdContent,rechaFixHolder.odRelat);
        rechaFixHolder.tvRechContent.setText("你当前即将充值 " + rechargeListItemBean.getNow() + " 元到您到还款账户");

        //立即結算监听
        rechaFixHolder.tvSettle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if(settleButtonInterface != null){
                    settleButtonInterface.onclick(view,position);
                }
            }
        });

        //立即还款
        rechaFixHolder.tvRepayMent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(repayMentButtonInterface != null){
                    repayMentButtonInterface.onclick(view,position);
                }
            }
        });

        //立即充值
        rechaFixHolder.tvRechage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(rechageButtonInterface != null){
                    rechageButtonInterface.onclick(view,position);
                }
            }
        });

        if(listRechargeItemBean.get(position).isLastPeriodIsOverdue()==true){
            rechaFixHolder.settlement_lay.setVisibility(View.GONE);
        }else {
            rechaFixHolder.settlement_lay.setVisibility(View.VISIBLE);
        }

    }


    private void setDataToText(List<OverListBean>  overListBeans,TextView textView,RelativeLayout relativeLayout){
        if(overListBeans != null && overListBeans.size() > 0){
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < overListBeans.size(); i++){
                String date = overListBeans.get(i).getShouldPayDate();  //日期
                float interest = overListBeans.get(i).getInterest();   //利息
                float principal = overListBeans.get(i).getPrincipal();       //本金
                float overdue = overListBeans.get(i).getOverdue();             //逾期费用
                float count = interest + principal + overdue;
                float  bcount   =  (float)(Math.round(count * 100))/100;               //这里的100就是2位小数点,如果要其它位,
                sb.append(date + "逾期金额 " + bcount + " 元");
                if(i != overListBeans.size()-1){
                    sb.append("\r\n");
                    sb.append("\r\n");
                }
            }
            textView.setText(sb);
        }else {
            relativeLayout.setVisibility(View.GONE);        //没有数据不显示
        }
    }



    @Override
    public int getItemCount() {
        return listRechargeItemBean == null ? 0 : listRechargeItemBean.size();
    }


    public class RechaFixHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_title_id)
        TextView tvTitle;
        @BindView(R.id.tv_content_id)
        TextView tvContent;
        @BindView(R.id.tv_settle_id)
        TextView tvSettle;
        @BindView(R.id.tv_odtitle_id)
        TextView tvOdTitle;
        @BindView(R.id.tv_odcontent_id)
        TextView tvOdContent;
        @BindView(R.id.tv_repayment_id)
        TextView tvRepayMent;
        @BindView(R.id.tv_rechtitle_id)
        TextView tvRechTitle;
        @BindView(R.id.tv_rccontent_id)
        TextView tvRechContent;
        @BindView(R.id.tv_rechage_id)
        TextView tvRechage;
        @BindView(R.id.rl_odrl_id)
        RelativeLayout odRelat;
        @BindView(R.id.settlement_lay)
        RelativeLayout settlement_lay;
        public RechaFixHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    public void setSettleButtonInterface(SettleButtonInterface mSettleButtonInterface){
        this.settleButtonInterface = mSettleButtonInterface;
    }

    public void setRepayMentButtonInterface(RepayMentButtonInterface mRepayMentButtonInterface){
        this.repayMentButtonInterface = mRepayMentButtonInterface;
    }


    public void setRechageButtonInterface(RechageButtonInterface mRechageButtonInterface){
        this.rechageButtonInterface = mRechageButtonInterface;
    }

    /**
     * 立即结算按钮点击事件对应的接口
     */
    public interface SettleButtonInterface{
        public void onclick( View view,int position);
    }


    /**
     * 立即还款按钮点击事件对应的接口
     */
    public interface RepayMentButtonInterface{
        public void onclick( View view,int position);
    }


    /**
     * 立即充值按钮点击事件对应的接口
     */
    public interface RechageButtonInterface{
        public void onclick( View view,int position);
    }


}
